var express = require('express');
var router = express.Router();
var http = require('http')
var https = require('https')
var {handleRequestByPromise} = require('../utils/ajax')
let cheerio = require('cheerio');
let url = 'https://m.biqugeu.net'
/* GET users listing. */
// 获取图书分类
// 掐头去尾
router.get('/categroy', async function (req, res, next) {
    let homeBody = await handleRequestByPromise({ url:url+'/sort.html' });
    let $ = cheerio.load(homeBody);
    let categroyList = []
    for(index in $('.content .prev a')){
        console.log(index)
        item = $('.content .prev a').eq(index)
        let obj = {
            name:item.html(),
            url:url+item.attr('href')
        }
        categroyList.push(obj)
        if(index == $('.content .prev a').length-1){
            break
        }
    }
    res.send({categroyList});
});
// 获取分类书籍
// 根据分类排序获取
router.get('/bookList', async function (req, res, next) {
    let {id} = req.query
    let homeBody = await handleRequestByPromise({ url:`${url}/${id}` });
    let bookList = JSON.parse(homeBody.toString())
    bookList.forEach(element => {
        element.url_list = url + element.url_list
        element.catalogId = element.url_img.replace(/.*\//,'').replace(/\..*/,'')
    });
    res.send(bookList);
});
// 获取书籍目录
// 根据图书封面图片id获取
router.get('/bookCatalog', async function (req, res, next) {
    let {id} = req.query
    let homeBody = await handleRequestByPromise({ url:`https://www.bbtxt8.com/json_book?id=${id}&page=0` });
    let catalogList = JSON.parse(homeBody.toString())
    res.send(catalogList);
});

// 获取图书详情根据url
router.get('/bookDetail', async function (req, res, next) {
    let {url} = req.query
    let homeBody = await handleRequestByPromise({ url });
    let $ = cheerio.load(homeBody);
    let detail = {
        img:$('.cover img').attr('src'),
        catalogId:$('.more a').attr('href').replace(/.*\(/,'').replace(/\).*/,''),
        name:$('.info h1').html(),
        author:$('.info .small span').eq(0).html(),
        last:$('.info .small span').eq(3).find('a').html(),
        updateTime:$('.info .small .last').eq(0).html(),
        status:$('.info .small span').eq(1).html(),
        description:$('.intro dd').html().replace(/\<span.*?\>/g,'').replace(/\<\/span.*?\>/g,'').replace(/展开全部.*/,'')
    }
    res.send(detail);
});

// 获取章节内容
// 根据章节index+1获取图书地址 例https://www.bbtxt8.com/book/3155/575.html
router.get('/bookContent', async function (req, res, next) {
    let {url} = req.query
    let homeBody = await handleRequestByPromise({ url });
    let $ = cheerio.load(homeBody);
    let content = $('#chaptercontent').html()
    let title = $('.content .wap_none').html()
    let prev = $('#pb_prev').attr('href').replace(/.*\//,'').replace('.html','')
    let nextPage = $('#pb_next').attr('href').replace(/.*\//,'').replace('.html','')
    res.send({title,content,prev,next:nextPage})
});

router.get('/search', async function (req, res, next) {
    let {content} = req.query
    content = encodeURIComponent(content)
    let homeBody = await handleRequestByPromise({ url:'https://www.bbtxt8.com/s?q='+content });
    let $ = cheerio.load(homeBody);
    let  list = []
    if($('.type_show .bookbox').length){
        for(index in $('.type_show .bookbox')){
            item = $('.type_show .bookbox').eq(index)
            let obj = {
                name:item.find('.bookname a').html(),
                img:item.find('.bookimg img').attr('src'),
                author:item.find('.bookinfo .author').html(),
                categroy:item.find('.bookinfo .cat').html(),
                updateTime:item.find('.bookinfo .uptime').html(),
                last:item.find('.bookinfo .update a').html(),
                bookLink:url+item.find('.bookname a').attr('href')
            }
            list.push(obj)
            if(index == $('.type_show .bookbox').length-1){
                break
            }
        }
    }
    
    res.send(list)
});
module.exports = router;